#ifndef _LOADER_H_
#define _LOADER_H_

#include <mutex>
#include <memory>
#include "dict.h"

template <typename Dict>
class Loader
{

public:
    using dict_type = Dict;
    using pointer_type = std::shared_ptr<Dict>;

    static Loader &get_instance()
    {
        static Loader instance;
        return instance;
    }

    auto operator->()
    {
        std::unique_lock<std::mutex> lk(_m);
        std::shared_ptr<Dict> pd(_dict);
        lk.unlock();
        return pd;
    }

    void load(const std::shared_ptr<Dict> &new_dict)
    {
        std::lock_guard<std::mutex> lk(_m);
        _dict = new_dict;
    }

    void load(std::shared_ptr<Dict> &&new_dict)
    {
        std::lock_guard<std::mutex> lk(_m);
        _dict = std::move(new_dict);
    }

private:
    Loader() = default;

    ~Loader() = default;

    Loader(const Loader &) = delete;

    Loader(Loader &&) = delete;

    Loader &operator=(const Loader &) = delete;

    Loader &operator=(Loader &&) = delete;

    std::mutex _m;
    std::shared_ptr<Dict> _dict;
};

#endif